﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class MechanicalWorkForm : Form
    {
        int regID, avpID, fhID, pfshID = -1, cultID;

        const string catTechniqe = "TechKind", catOperation = "OperationKind", catTechTool = "TechToolKind", catDimension = "Dimension";

        RESP_IIWaterAssessment.Entity.RESP_addDataContext resp_data = new RESP_IIWaterAssessment.Entity.RESP_addDataContext(DBClass.RESPContext.Connection);
        RESP_IIWaterAssessment.Entity.tb_mechwork MechWork;

        public MechanicalWorkForm()
        {
            InitializeComponent();
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbFH.DataSource = null;
                cbPFSH.DataSource = null;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);

            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbFH.SelectedIndexChanged -= cbFH_SelectedIndexChanged;
                cbFH.Enabled = true;
                cbFH.DataSource = DBClass.Farms
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbFH.DisplayMember = "Name";
                cbFH.ValueMember = "ID";
                cbFH.SelectedIndex = -1;
                cbFH.Text = string.Empty;
                cbPFSH.DataSource = null;
                cbFH.SelectedIndexChanged += new EventHandler(cbFH_SelectedIndexChanged);
            }
        }

        private void cbFH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFH.SelectedIndex >= 0)
            {
                fhID = Convert.ToInt32(cbFH.SelectedValue);
                cbPFSH.Enabled = true;
                cbPFSH.SelectedIndexChanged -= cbPFSH_SelectedIndexChanged;
                cbPFSH.DataSource = DBClass.Schools
                    .Where(wh => wh.FarmID == fhID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbPFSH.DisplayMember = "Name";
                cbPFSH.ValueMember = "ID";
                cbPFSH.SelectedIndex = -1;
                cbPFSH.Text = string.Empty;
                cbPFSH.SelectedIndexChanged += new EventHandler(cbPFSH_SelectedIndexChanged);
            }
        }

        private void cbPFSH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbPFSH.SelectedIndex >= 0)
            {
                pfshID = Convert.ToInt32(cbPFSH.SelectedValue);
                groupBox1.Enabled = true;
                if (cultID > 0 && pfshID > 0)
                    ShowWork(cultID, pfshID, dtpDate.Value.Date);
            }
            else
            {
                pfshID = 0;
                groupBox1.Enabled = false;
            }
        }
        // разобраться с уд.Измерением (Dimtntion) - добавить в категории и изменить тип
        private void ShowWork(int cultID, int pfshID, DateTime dateTime)
        {
            MechWork = resp_data.tb_mechworks
                .Where(wh => wh.Date == dateTime && wh.CultureID == cultID && wh.SchoolID == pfshID)
                .SingleOrDefault();
            if (MechWork != null)
            {
                tbArea.Text = MechWork.Area.ToString();
                //tbDimension.Text = MechWork.Dimension.Value.ToString();
                tbHourQuantity.Text = MechWork.HourQuantity.Value.ToString();
                tbOperationCost.Text = MechWork.OperationCost.Value.ToString();
                cbOperation.SelectedIndex = cbOperation.FindStringExact(UtilClass.GetCategoryDisplayName((int)MechWork.Operation, catOperation));
                cbTechKind.SelectedIndex = cbTechKind.FindStringExact(UtilClass.GetCategoryDisplayName((int)MechWork.TechKind, catTechniqe));
                cbTool.SelectedIndex = cbTool.FindStringExact(UtilClass.GetCategoryDisplayName((int)MechWork.Tool, catTechTool));
                cbDimension.SelectedIndex = cbDimension.FindStringExact(UtilClass.GetCategoryDisplayName((int)MechWork.Dimension, catDimension));

            }
            else
            {
                tbArea.Text = tbHourQuantity.Text = tbOperationCost.Text = String.Empty;
                cbDimension.SelectedIndex = cbOperation.SelectedIndex = cbTechKind.SelectedIndex =
                    cbTool.SelectedIndex = -1;
                cbTool.Text = cbTechKind.Text = cbOperation.Text = cbDimension.Text = string.Empty;
            }

        }

        private void MechanicalWorkForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            cbCulture.SelectedIndexChanged -= cbCulture_SelectedIndexChanged;
            cbCulture.DataSource = UtilClass.GetCBCultureDataSourse();
            cbCulture.DisplayMember = "Name";
            cbCulture.ValueMember = "ID";
            cbCulture.SelectedIndex = -1;
            cbCulture.SelectedIndexChanged += new EventHandler(cbCulture_SelectedIndexChanged);

            FillCB();
        }

        private void FillCB()
        {
            cbOperation.DataSource = UtilClass.FillCBCategory(catOperation);
            cbTechKind.DataSource = UtilClass.FillCBCategory(catTechniqe);
            cbTool.DataSource = UtilClass.FillCBCategory(catTechTool);
            cbDimension.DataSource = UtilClass.FillCBCategory(catDimension);

            cbDimension.DisplayMember = cbTool.DisplayMember = cbTechKind.DisplayMember = cbOperation.DisplayMember = "Name";
            cbDimension.ValueMember = cbOperation.ValueMember = cbTechKind.ValueMember = cbTool.ValueMember = "ID";

            cbDimension.SelectedIndex = cbTool.SelectedIndex = cbTechKind.SelectedIndex = cbOperation.SelectedIndex = -1;
            cbOperation.Text = cbTechKind.Text = cbTool.Text = string.Empty;
        }

        private void cbCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCulture.SelectedIndex >= 0)
            {
                cultID = Convert.ToInt32(cbCulture.SelectedValue);
                if (cultID > 0 && pfshID > 0)
                    ShowWork(cultID, pfshID, dtpDate.Value.Date);
            }
            else
                cultID = 0;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {

            if (pfshID > 0 && cultID > 0 && cbDimension.SelectedIndex >= 0 && cbOperation.SelectedIndex >= 0 && cbTechKind.SelectedIndex >= 0 && cbTool.SelectedIndex >= 0)
            {
                MechWork = resp_data.tb_mechworks
                    .Where(wh => wh.Date == dtpDate.Value.Date && wh.CultureID == cultID && wh.SchoolID == pfshID)
                    .SingleOrDefault();
                decimal area, opCost;
                decimal hQuant;

                if (tbArea.Text.GetDecimalOrDefault(out area) &&
                tbOperationCost.Text.GetDecimalOrDefault(out opCost) &&
                decimal.TryParse(tbHourQuantity.Text, out hQuant))
                {

                    if (MechWork != null)
                    {
                        MechWork.Area = area;
                        MechWork.CultureID = cultID;
                        MechWork.Date = dtpDate.Value.Date;
                        MechWork.Dimension = Convert.ToInt32(cbDimension.SelectedValue);
                        MechWork.HourQuantity = hQuant;
                        MechWork.Operation = Convert.ToInt32(cbOperation.SelectedValue);
                        MechWork.OperationCost = opCost;
                        MechWork.TechKind = Convert.ToInt32(cbTechKind.SelectedValue);
                        MechWork.Tool = Convert.ToInt32(cbTool.SelectedValue);
                    }
                    else
                    {
                        MechWork = new RESP_IIWaterAssessment.Entity.tb_mechwork
                        {
                            Area = area,
                            CultureID = cultID,
                            Date = dtpDate.Value.Date,
                            Dimension = Convert.ToInt32(cbDimension.SelectedValue),
                            HourQuantity = hQuant,
                            Operation = Convert.ToInt32(cbOperation.SelectedValue),
                            OperationCost = opCost,
                            TechKind = Convert.ToInt32(cbTechKind.SelectedValue),
                            Tool = Convert.ToInt32(cbTool.SelectedValue),
                            SchoolID = pfshID
                        };
                        resp_data.tb_mechworks.InsertOnSubmit(MechWork);
                    }
                    try
                    {
                        resp_data.SubmitChanges();
                        MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                    }
                    catch
                    {
                        MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                    }
                }
            }
            else
            {
                if (InformationClass.Lang)
                    MessageBox.Show("Не верно заполненны данные. Проверьте правильность заполнения.");
            }
        }

        private void dtpDate_ValueChanged(object sender, EventArgs e)
        {
            if (cultID > 0 && pfshID > 0)
                ShowWork(cultID, pfshID, dtpDate.Value.Date);
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
    }
}
